/**
 * Copyright (c) Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package ronp.anim.gui.tools;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import ronp.anim.data.filmstrip.Animation;
import ronp.anim.gui.Gui;
import ronp.anim.gui.SelectedState;
import ronp.anim.gui.viewer.AnimViewer;
import ronp.anim.main.Main;
import ronp.anim.saving.SaveHandler;

/**
 * The side menu on the right side of the screen with transforms and draws and such.
 * Basically a huge mass of spaghetti.
 * 
 * @author Daniel Centore
 *
 */
public class ToolsMenu extends JPanel
{
	private static final long serialVersionUID = 1L;

	private File myFile = null;
	private Image lastBackground;
	private final JSpinner zoomSpin;
	private final Animation anim;

	public ToolsMenu(final AnimViewer viewer, final Animation anim, final SelectedState state)
	{
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.anim = anim;

		zoomSpin = new JSpinner(new SpinnerNumberModel(100, 1, 1000, 1));
		zoomSpin.addChangeListener(new ChangeListener()
		{
			@Override
			public void stateChanged(ChangeEvent e)
			{
				try
				{
					viewer.setZoom((float) (((double) (Integer) zoomSpin.getValue()) / 100));
				} catch (Exception e1)
				{
					// occurs on some weird rounding cases. whatever.
				}
			}
		});
		JPanel zoom = new JPanel();
		zoom.add(new JLabel("Zoom:"));
		zoom.add(zoomSpin);
		this.add(zoom);

		final JCheckBox grips = new JCheckBox("Use Grips");
		grips.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				viewer.setUseGrips(grips.isSelected());
				viewer.repaint();
			}
		});
		grips.setSelected(true);
		this.add(grips);

		final JButton colorChooser = new JButton("Choose Color");
		colorChooser.setBackground(state.getActiveColor());
		colorChooser.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				Color c = JColorChooser.showDialog(ToolsMenu.this, "Choose Color", state.getActiveColor());
				//				Color c = ColorPicker.showDialog(null, state.getActiveColor());
				if (c != null)
				{
					state.setActiveColor(c);
					colorChooser.setBackground(state.getActiveColor());
				}
			}
		});

		this.add(colorChooser);


		JButton save = new JButton("Save");
		save.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				saveAction();
			}
		});
		this.add(save);

		JButton open = new JButton("Open");
		open.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser choose = SaveHandler.fileChooser(SaveHandler.FILETYPE_NPA);
				int val = choose.showOpenDialog(null);
				if (val == JFileChooser.APPROVE_OPTION)
				{
					File k = choose.getSelectedFile(); // TODO: pass this on to the new SideMenu when it gets created
					new Gui(SaveHandler.loadAnimation(k));
				}
			}
		});
		this.add(open);

		final JSpinner widthSpin = new JSpinner(new SpinnerNumberModel(Main.DEFAULT_ANIM_WIDTH, 1, 1000, 1));
		widthSpin.addChangeListener(new ChangeListener()
		{
			@Override
			public void stateChanged(ChangeEvent e)
			{
				anim.setWidth((Integer) widthSpin.getValue());
			}
		});
		JPanel width = new JPanel();
		width.add(new JLabel("Width:"));
		width.add(widthSpin);
		this.add(width);

		final JSpinner heightSpin = new JSpinner(new SpinnerNumberModel(Main.DEFAULT_ANIM_HEIGHT, 1, 1000, 1));
		heightSpin.addChangeListener(new ChangeListener()
		{
			@Override
			public void stateChanged(ChangeEvent e)
			{
				anim.setHeight((Integer) heightSpin.getValue());
			}
		});
		JPanel height = new JPanel();
		height.add(new JLabel("Height:"));
		height.add(heightSpin);
		this.add(height);

		final JCheckBox showBackground = new JCheckBox("Show Background");
		showBackground.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if (showBackground.isSelected())
					viewer.setUnderlayer(lastBackground);
				else
				{
					lastBackground = viewer.getUnderlayer();
					viewer.setUnderlayer(null);
				}

				viewer.repaint();
			}
		});
		this.add(showBackground);
		showBackground.setSelected(true);

		JButton background = new JButton("Load Background");
		background.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser choose = SaveHandler.fileChooser(SaveHandler.FILETYPE_IMAGES);
				int val = choose.showOpenDialog(null);
				if (val == JFileChooser.APPROVE_OPTION)
				{
					File k = choose.getSelectedFile();
					viewer.setUnderlayer(new ImageIcon(k.getAbsolutePath()).getImage());
					viewer.repaint();
				}
			}
		});
		this.add(background);

		final JCheckBox showFade = new JCheckBox("Show Fade");
		showFade.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				viewer.setShowFade(showFade.isSelected());

				viewer.repaint();
			}
		});
		this.add(showFade);
		showFade.setSelected(true);

		viewer.settMenu(this);
	}

	public void saveAction()
	{
		if (myFile != null)
			SaveHandler.saveAnimation(anim, myFile);
		else
		{
			JFileChooser choose = SaveHandler.fileChooser(SaveHandler.FILETYPE_NPA);
			int val = choose.showSaveDialog(null);
			if (val == JFileChooser.APPROVE_OPTION)
			{
				File k = choose.getSelectedFile();
				String s = k.getAbsolutePath();
				if (!s.toLowerCase().endsWith("." + SaveHandler.EXTENSION.toLowerCase()))
					s += "." + SaveHandler.EXTENSION.toLowerCase();

				SaveHandler.saveAnimation(anim, myFile = new File(s));
			}

		}
	}

	public JSpinner getZoomSpin()
	{
		return zoomSpin;
	}
}
